﻿/**
 * @brief     鏂囦欢閫氱敤宸ュ叿绫?
 * @details   鏀寔鏂囦欢鐨勫姞瀵嗐€佽В瀵嗐€佹牎楠岀瓑鎿嶄綔
 * @author    Yin
 * @version   1.0
 * @date
 * @copyright
 * @note
 * 浣跨敤瀹炰緥
 *  QString strInifile = QString("hahaya.ini");
 *  QSettings *configIniWrite = new QSettings(strInifile, QSettings::IniFormat);
 *  Singleton<Common::FileTool>::instance()->setSettings(configIniWrite);

 *  Singleton<Common::FileTool>::instance()->getValue("Network","Ip", "127.0.0.1");
 *  Singleton<Common::FileTool>::instance()->setValue("Network","Ip", "127.0.0.15");
 *  QVariant鐨勬暟鎹被鍨嬭浆鍖栬鏌ョ湅 QVariant绫昏鏄?

 */

#ifndef FILETOOL_H
#define FILETOOL_H

#include "common_global.h"

#include <QObject>
#include <QFile>
#include <QDataStream>
#include <QTextStream>
#include <QSettings>

#include <QTemporaryDir>            // 鍒涘缓涓存椂鏂囦欢澶?
#include <QTemporaryFile>           // 鍒涘缓涓存椂鏂囦欢
#include <QFileSystemWatcher>       // 鏂囦欢鍜岀洰褰曠洃鍚被

namespace Common {

	class COMMONSHARED_EXPORT FileTool : public QObject
	{
		Q_OBJECT

	public:
		explicit FileTool(QObject *parent = nullptr);
		~FileTool();

	public:
		/**
		* @brief writeDataFile 鍐欐暟鎹枃浠讹紙浜岃繘鍒讹紙QByteArray锛夛級
		* @param filePath 鏂囦欢璺緞
		* @param buff 鍐欏叆鐨勬暟鎹?
		* @return
		* @note 璇ユ帴鍙ｆ槸鐩存帴鍐欐搷浣滐紙鍗曠嫭浣跨敤锛?
		*/
		static bool writeDataFile(QString filePath, QByteArray buff);

		/**
		* @brief readDataFile 璇绘暟鎹枃浠讹紙浜岃繘鍒讹紙QByteArray锛夛級
		* @param filePath 鏂囦欢璺緞
		* @param buff 璇诲彇鐨勬暟鎹?
		* @return
		* @note 璇ユ帴鍙ｆ槸鐩存帴璇绘搷浣滐紙鍗曠嫭浣跨敤锛?
		*/
		static bool readDataFile(QString filePath, QByteArray & buff);

		/*!< 鎿嶄綔.ini 鏂囦欢 鍏ㄥ眬INI璁块棶 */
		/**
		 * @brief	setInilSettings 璁剧疆Ini鏂囦欢鎿嶄綔绫?
		 * @param  
		 * @return 
		 */
		static void setInilSettings(QSettings * settings);

		/**
		 * @brief	getIniSettings 鑾峰彇Ini鏂囦欢鎿嶄綔绫?
		 * @return 
		 */
		static QSettings * getIniSettings();

		/**
		* @brief setValue 璁剧疆鍏ㄥ眬ini閰嶇疆鏂囦欢涓瓧娈靛唴瀹?
		* @param group 鍒嗙粍鍚嶇О
		* @param key 寰呮煡璇㈠€糼ey
		* @param isForce 鏄惁寮哄埗鍐欏叆
		* @param defaultValue 瀛楁鍊?
		*/
		static void setIniValue(const QString & group, const QString &key, const QVariant &value, const bool isForce = true);

		/**
		* @brief getValue 鏌ヨ鍏ㄥ眬ini閰嶇疆鏂囦欢涓瓧娈靛唴瀹?
		* @param group 鍒嗙粍鍚嶇О
		* @param key   寰呮煡璇㈠€糼ey
		* @param value 榛樿鍊硷紝鑻roup涓嬩笉瀛樺湪瀵瑰簲鐨刱ey锛屼娇鐢ㄩ粯璁ゅ€间唬鏇?
		* @return
		*/
		static QVariant getIniValue(const QString & group, const QString &key, const QVariant &value = QVariant());

		/*!
		 * @brief 瀵瑰瓧绗︿覆杩涜MD5鍔犲瘑
		 * @param[in] text 寰呭姞瀵嗙殑瀛楃涓?
		 * @return 鍔犲瘑鍚庣殑瀛楃涓?
		 */
		static QString MD5(QString text);

		/*!
		 * @brief 鑾峰彇鏂囦欢鐨凪D5淇℃伅
		 * @details 瀵规枃浠惰繘琛孧D5璁＄畻锛岀敤浜庣綉缁滀紶杈撳悗瀵规帴鏀剁殑鏂囦欢鏁版嵁杩涜姝ｇ‘鎬ф牎楠岋紝闃叉浼犺緭杩囩▼涓敊璇垨鑰呰绡℃敼锛?
		 * @param[in] fileName 鏂囦欢鍚嶄俊鎭?
		 * @return 璁＄畻鍚庣殑MD5鍊间俊鎭?
		 */
		static QString MD5File(QString fileName);

	private:
		static QSettings * g_iniSettings;
	};

}//namespace Common

#endif // FILETOOL_H
